package com.wyclabs.auth.common.config;

import com.wyclabs.auth.common.SecurityAccessTokenConverter;
import com.wyclabs.auth.common.constants.CommonConstants;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

/**
 * token存储配置
 * @author rs
 * @date 2019/10/16
 */
@Configuration
@Slf4j
@AllArgsConstructor
public class TokenStoreConfig {

    private final RedisConnectionFactory redisConnectionFactory;

    private final SecurityAccessTokenConverter securityAccessTokenConverter;

    /**
     * 指定token存储方式
     */
    @Bean
    public RedisTokenStore redisTokenStore() {
        return new RedisTokenStore(redisConnectionFactory);
    }

    /**
     * 指定JWT转化方式
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        jwtAccessTokenConverter.setSigningKey(CommonConstants.JWT_SIGN_KEY);
        jwtAccessTokenConverter.setAccessTokenConverter(securityAccessTokenConverter);
        return jwtAccessTokenConverter;
    }
}
